package com.atguigu.gmall.search.service.impl;

import com.atguigu.gmall.search.Goods;
import com.atguigu.gmall.search.repository.GoodsRepository;
import com.atguigu.gmall.search.service.GoodsService;
import com.atguigu.gmall.search.vo.SearchParamVo;
import com.atguigu.gmall.search.vo.SearchResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsRepository goodsRepository;


    @Autowired
    ElasticsearchRestTemplate searchTemplate;

    @Override
    public void save(Goods goods) {
        goodsRepository.save(goods);
    }

    @Override
    public void deleteById(Long skuId) {
        goodsRepository.deleteById(skuId);
    }

    @Override
    public SearchResultVo search(SearchParamVo searchParam) {


        //TODO 1、准备一个Query对象。DSL： 根据前端传来的所有条件，构建一个复杂的DSL语句【就是Query对象】，
        Query query = buildQuery(searchParam);

        //2、搜索
        SearchHits<Goods> goods = searchTemplate.search(query, Goods.class, IndexCoordinates.of("goods"));

        //TODO 3、根据搜索得到的结果，将数据封装成页面能用的vo
        SearchResultVo result = buildResult(goods);

        return result;
    }


    /**
     * 根据检索结果构建 页面数据
     * @param goods
     * @return
     */
    private SearchResultVo buildResult(SearchHits<Goods> goods) {
        return null;
    }


    /**
     * 根据前端参数构建query对象
     * @param searchParam
     * @return
     */
    private Query buildQuery(SearchParamVo searchParam) {
        return null;
    }
}
